package com.tuling.timer;


import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TimerTest {

    public static void main(String[] args) {
        Timer t = new Timer(); //创建对象的时候，任务启动
        for (int i = 0; i < 2; i++) {
            TimerTask timerTask = new FooTimerTask("foo" + i);
            t.schedule(timerTask, new Date(), 2000); //任务添加
        }

    }

}


class FooTimerTask extends TimerTask {

    private String name;

    public FooTimerTask(String name) {
        this.name = name;
    }


    @Override
    public void run() {
        try {
            System.out.println("name=" + name + ",startTime" + new Date());
            Thread.sleep(3000);
            System.out.println("name=" + name + ",endTime" + new Date());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
